#25 to life parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class TwoFivetoLife_DBL(Model3D):

    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(TwoFivetoLife_DBL,self).__init__("DBL")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
        
    def read_name(self):
        
        name = ""
        char = self.inFile.read_char()
        while char != "\x00":
            name = ''.join([name, char])
            char = self.inFile.read_char()
        return name
        
    def parse_texture(self, name, width, height):
        
        data = width*height*2
        print data
        self.inFile.seek(data, 1)
        
    def parse_materials(self):
    
        numMat = self.inFile.read_long()
        self.inFile.read_long(5)
        name = self.read_name()
        
        print "%d materials" %numMat
        print name
        
        material = []        
        for i in range(numMat):
            self.inFile.read_long(4)
            width, height = self.inFile.read_short(2)
            self.inFile.read_long(5)
            texName = self.read_name()
            material.append([texName, width, height])
            
        for i in range(numMat):
            mat = material[i]
            self.parse_texture(mat[0], mat[1], mat[2])
        
    def parse_vertices(self, meshName, numVerts, vertSize):
        
        print "%d verts" %numVerts, vertSize
        if vertSize == 32:
            for i in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
                
                
        elif vertSize == 40:
            for i in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                self.inFile.read_float(7)
                
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
        else:
            print "unknown vert size", vertSize
    
    def parse_faces(self, numFaces):
        
        print "%d faces" %numFaces
        for i in range(numFaces):
            print self.inFile.read_short(3)
    
    def parse_mesh(self, chunkSize):
        
        size = 0
        print self.inFile.tell()
        self.inFile.seek(236, 1)
        numIndices = self.inFile.read_long()
        unk = self.inFile.read_long()
        
        print "%d indices" %numIndices
        #self.parse_faces(numIndices)
        for i in range(numIndices):
            self.inFile.read_short()
            
        self.inFile.read_long()
        numVerts = self.inFile.read_long()
        vertSize = self.inFile.read_long()
        vertChunk = self.inFile.read_long()
        
        meshName = "temp"
        self.create_object(meshName)
        self.parse_vertices(meshName, numVerts, vertSize)
    
        size += 260
        size += numIndices * 2
        size += vertChunk
        remain = chunkSize - size
        self.inFile.seek(remain, 1)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        self.inFile.read_long(6)
        numChunks = self.inFile.read_long()
        self.inFile.read_long()
        
        for i in range(numChunks):
            
            type1 = self.inFile.read_long()
            chunkSize = self.inFile.read_long()
            chunkType = self.inFile.read_short()
            self.inFile.read_string(4)
            self.inFile.seek(50, 1)
            
            print chunkType, chunkSize
            if chunkType == 0x0E0082:
                self.inFile.seek(chunkSize, 1)
                #self.parse_materials()
            elif chunkType == 0x0E0067:
                self.inFile.seek(chunkSize, 1)
            elif type1 in [0x0E0022, 0x0E0023]:
                self.parse_mesh(chunkSize)
            else:
                self.inFile.seek(chunkSize, 1)
                
            
        print self.inFile.filesize()
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = TwoFivetoLife_DBL(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "DBL", "25 to life DBL"

if __name__ == '__main__':
    
    files = ["BUTTERFLY.DBL", "CELLPHONE.DBL", "COLT1911.DBL", "MP_TIJUANA.DBL"]
    obj = read_file(files[0])